﻿using System;
using System.Collections.Generic;

using Microsoft.WowAddonStudio.Lua.Parser;
using Microsoft.WowAddonStudio.Lua.Support;

namespace Microsoft.WowAddonStudio.Lua.AST
{
    /// <summary>
    /// Represents an assignment statement in a lua code file.
    /// </summary>
    /// <example>
    /// <code>x, y = 5, 6</code>
    /// </example>
    public class Assignment : Statement
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Assignment"/> class.
        /// </summary>
        /// <param name="location">The location of the node.</param>
        public Assignment(LexLocation location)
            : base(location)
        {
        }

        /// <summary>
        /// Gets or sets the linked list of variables on the left side of the assignment.
        /// </summary>
        public Node VariableList { get; set; }

        /// <summary>
        /// Gets or sets the linked list of expression on the right side of the assignment.
        /// </summary>
        public Node ExpressionList { get; set; }

        /// <summary>
        /// Gets or sets whether the assignment is local.
        /// </summary>
        public bool IsLocal { get; set; }

        /// <summary>
        /// Gets the assignments as single node pairs.
        /// </summary>
        /// <returns>An instance of the <see cref="ParallelEnumerator{TFirst,TSecond}"/> class.</returns>
        public IEnumerable<KeyValuePair<Node, Node>> GetSingleAssignments()
        {
            var enumerator = new ParallelEnumerator<Node, Node>(VariableList, ExpressionList);
            while (enumerator.MoveNext())
                yield return new KeyValuePair<Node, Node>(enumerator.CurrentFirst, enumerator.CurrentSecond);
        }

        /// <summary>
        /// Gets the child nodes of the node.
        /// </summary>
        /// <returns>An enumerable collection of nodes.</returns>
        public override IEnumerable<Node> GetChildNodes()
        {
            yield return VariableList;
            yield return ExpressionList;
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return String.Format("Assignment [IsLocal = {0}]", this.IsLocal);
        }
	}
}
